SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


CREATE VIEW dbo.[DnnProduct_vw_Users]
AS
	SELECT DISTINCT 
		U.UserID, 
		(CASE WHEN IsSuperUser = 1 THEN NULL ELSE UP.PortalId END) AS PortalId, 
		U.Username, 
		U.FirstName, 
		U.LastName, 
        U.DisplayName, 
		U.IsSuperUser, 
		U.Email, 
		U.AffiliateId, 
		U.UpdatePassword, 
		UP.Authorised, 
		(CASE WHEN UP.PortalId IS NULL THEN U.IsDeleted ELSE UP.IsDeleted END) AS IsDeleted, 
		UP.RefreshRoles, 
		U.LastIPAddress, 
		U.CreatedByUserID, 
		U.CreatedOnDate, 
		U.LastModifiedByUserID, 
        U.LastModifiedOnDate
	FROM dbo.DnnProduct_Users AS U 
		LEFT OUTER JOIN dbo.DnnProduct_UserPortals AS UP ON U.UserID = UP.UserId
GO
